<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    
  </style>
</head>
<body>
  <h3>登录</h3>
  <input type="text" class="un"><br>
  <input type="text" class="pw"><br>
  <input type="button" class="btn" value="登录">
  <a href="./1注册.html">去注册</a>
</body>
<script src="./ajax.js"></script>
<script src="../toast.js"></script>
<script>


  const userMsg = JSON.parse(sessionStorage.getItem("userMsg")) || {};
  $(".un").value = userMsg.username || "";
  $(".pw").value = userMsg.password || "";

  

  $(".btn").onclick = function(){
    ajax({
      url:"http://10.11.50.46:3000/login",
      data:{
        username: $(".un").value,
        password: $(".pw").value
      },
      success:res=>{
        res = JSON.parse(res)
        if(res.code === 1){
          Toast.init({
            type: "success",
            position:"center",
            title: res.msg,
            close: res=>{
              console.log("跳转到首页")
            }
          })
        }else if(res.code === 0){
          Toast.init({
            type: "error",
            position:"center",
            title: res.msg,
            close: res=>{
              $(".un").value = "";
              $(".un").focus()
            }
          })
        }else if(res.code === 2){
          Toast.init({
            type: "error",
            position:"center",
            title: res.msg,
            close: res=>{
              $(".pw").value = "";
              $(".pw").focus()
            }
          })
        }
      }
    })
  }



  function $(s){
    return document.querySelector(s)
  }

</script>
</html>